﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SMplusXTeam.SyncMoped.SyncGurke;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;

namespace SMplusXTeam.SyncMoped.SyncGurkeTest
{
    [TestClass]
    public class FileHelperTest
    {



        /// <summary>
        ///A test for GetFiles
        ///</summary>
        [TestMethod()]
        public void GetFilesTest()
        {
            string baseDirectory = string.Empty;
            try
            {
                FileHelper.GetFiles(baseDirectory);
                Assert.Fail();
            }
            catch (ArgumentNullException)
            {
                //ok
            }
        }

        /// <summary>
        ///A test for GetFiles
        ///</summary>
        [TestMethod()]
        public void GetFilesTest1()
        {
            string baseDirectory = @"C:\";
            IEnumerable<string> actual;
            actual = FileHelper.GetFiles(baseDirectory);
            Assert.IsTrue(actual.LongCount() > 10);
            Console.WriteLine("Files count: " + actual.LongCount());
        }

        /// <summary>
        ///A test for GetFiles
        ///</summary>
        [TestMethod()]
        public void GetFilesTest2()
        {
            string baseDirectory = @"asdffiuaweruiqwer";
            try
            {
                IEnumerable<string> actual;
                actual = FileHelper.GetFiles(baseDirectory);
                Assert.Fail();
            }
            catch (ArgumentException)
            {
                //ok
            }
        }


        /// <summary>
        ///A test for IsFileBackedUp
        ///</summary>
        [TestMethod()]
        public void IsFileBackedUpTest()
        {
            FileInfo file1 = new FileInfo(@"c:\Windows\System32\imageres.dll");
            FileInfo file2 = new FileInfo(@"c:\Windows\System32\imageres.dll");
            ComparisonMethod comparisonMethod = ComparisonMethod.Md5;
            bool expected = true;
            bool actual = FileHelper.IsFileBackedUp(file1, file2, comparisonMethod);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        /// A test for GetMaxVersion
        /// </summary>
        [TestMethod]
        public void GetMaxVersionTest()
        {
            String filename = "lala.txt";
            String path = @"C:\Windows\Temp";
            int result = (int)FileHelper.GetMaxVersion(filename, path);
            Assert.AreEqual(0, result);
        }

        /// <summary>
        /// A test for GetMaxVersion
        /// </summary>
        [TestMethod]
        public void GetMaxVersionTest1()
        {
            String filename = "lala1.txt1";
            String path = @"C:\Windows\Temp";
            using (FileStream jo = File.Create(Path.Combine(path, filename)))
            {
            }
            int result = (int)FileHelper.GetMaxVersion(filename, path);
            File.Delete(Path.Combine(path, filename));
            Assert.AreEqual(0, result);

        }

        /// <summary>
        /// A test for GetMaxVersion
        /// </summary>
        [TestMethod]
        public void GetMaxVersionTest2()
        {
            String filename = "lala2.txt";
            String path = @"C:\Windows\Temp";
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".1")))
            {
            }
            int result = (int)FileHelper.GetMaxVersion(filename, path);
            File.Delete(Path.Combine(path, filename));
            Assert.AreEqual(1, result);
        }

        /// <summary>
        /// A test for GetMaxVersion
        /// </summary>
        [TestMethod]
        public void GetMaxVersionTest3()
        {
            String filename = "lala3.txt";
            String path = @"C:\Windows\Temp";
            File.Create(Path.Combine(path, filename));
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".1")))
            {
            }
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".2")))
            {
            }
            int result = (int)FileHelper.GetMaxVersion(filename, path);
            File.Delete(Path.Combine(path, filename + ".1"));
            File.Delete(Path.Combine(path, filename + ".2"));
            Assert.AreEqual(2, result);
        }

        /// <summary>
        /// A test for MoveBackedUpFiles
        /// </summary>
        [TestMethod]
        public void MoveBackedUpFilesTest()
        {
            String filename = "lala4.txt";
            String path = @"C:\Windows\Temp";
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".1")))
            {
            }
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".2")))
            {
            }
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".3")))
            {
            }
            FileHelper.MoveBackedUpFiles(filename, path, 5);
            Assert.IsTrue(!File.Exists(Path.Combine(path, filename + ".1")));
            Assert.IsTrue(File.Exists(Path.Combine(path, filename + ".2")));
            Assert.IsTrue(File.Exists(Path.Combine(path, filename + ".3")));
            Assert.IsTrue(File.Exists(Path.Combine(path, filename + ".4")));
            File.Delete(Path.Combine(path, filename + ".2"));
            File.Delete(Path.Combine(path, filename + ".3"));
            File.Delete(Path.Combine(path, filename + ".4"));
        }

        /// <summary>
        /// A test for MoveBackedUpFiles
        /// </summary>
        [TestMethod]
        public void MoveBackedUpFilesTest1()
        {
            String filename = "lala5.txt";
            String path = @"C:\Windows\Temp";
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".1")))
            {
            }
            FileHelper.MoveBackedUpFiles(filename, path, 5);
            Assert.IsTrue(!File.Exists(Path.Combine(path, filename + ".1")));
            Assert.IsTrue(File.Exists(Path.Combine(path, filename + ".2")));
            File.Delete(Path.Combine(path, filename + ".2"));
        }

        /// <summary>
        /// A test for MoveBackedUpFiles
        /// </summary>
        [TestMethod]
        public void MoveBackedUpFilesTest2()
        {
            String filename = "lala6.txt";
            String path = @"C:\Windows\Temp";
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".1")))
            {
            }
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".2")))
            {
            }
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".3")))
            {
            }
            FileHelper.MoveBackedUpFiles(filename, path, 1);
            Assert.IsTrue(!File.Exists(Path.Combine(path, filename + ".1")));
            Assert.IsTrue(!File.Exists(Path.Combine(path, filename + ".2")));
            Assert.IsTrue(!File.Exists(Path.Combine(path, filename + ".3")));
            Assert.IsTrue(!File.Exists(Path.Combine(path, filename + ".4")));
        }

        /// <summary>
        /// A test for MoveBackedUpFiles
        /// </summary>
        [TestMethod]
        public void MoveBackedUpFilesTest3()
        {
            String filename = "lala7.txt";
            String path = @"C:\Windows\Temp";
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".1")))
            {
            }
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".2")))
            {
            }
            using (FileStream jo = File.Create(Path.Combine(path, filename + ".3")))
            {
            }
            FileHelper.MoveBackedUpFiles(filename, path, 4);
            Assert.IsTrue(!File.Exists(Path.Combine(path, filename + ".1")));
            Assert.IsTrue(File.Exists(Path.Combine(path, filename + ".2")));
            Assert.IsTrue(File.Exists(Path.Combine(path, filename + ".3")));
            Assert.IsTrue(File.Exists(Path.Combine(path, filename + ".4")));
            File.Delete(Path.Combine(path, filename + ".2"));
            File.Delete(Path.Combine(path, filename + ".3"));
            File.Delete(Path.Combine(path, filename + ".4"));
        }

        /// <summary>
        /// A test for MoveBackedUpFiles
        /// </summary>
        [TestMethod]
        public void MoveBackedUpFilesTest4()
        {
            String filename = "lala8.txt";
            String path = @"C:\Windows\Temp";
            FileHelper.MoveBackedUpFiles(filename, path, 4);
            Assert.IsTrue(!File.Exists(Path.Combine(path, filename + ".1")));
        }



        /// <summary>
        ///A test for IsFileBackedUp
        ///</summary>
        [TestMethod()]
        public void IsFileBackedUpTest1()
        {
            FileInfo file1 = new FileInfo(@"c:\Windows\System32\imageres.dll");
            FileInfo file2 = new FileInfo(@"c:\Windows\System32\mshtml.dll");
            ComparisonMethod comparisonMethod = ComparisonMethod.Md5;
            bool expected = false;
            bool actual = FileHelper.IsFileBackedUp(file1, file2, comparisonMethod);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsFileBackedUp
        ///</summary>
        [TestMethod()]
        public void IsFileBackedUpTest3()
        {
            FileInfo file1 = new FileInfo(@"c:\Windows\System32\imageres.dll");
            FileInfo file2 = new FileInfo(@"c:\Windows\System32\imageres.dll");
            ComparisonMethod comparisonMethod = ComparisonMethod.Byte;
            bool expected = true;
            bool actual = FileHelper.IsFileBackedUp(file1, file2, comparisonMethod);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsFileBackedUp
        ///</summary>
        [TestMethod()]
        public void IsFileBackedUpTest4()
        {
            FileInfo file1 = new FileInfo(@"c:\Windows\System32\imageres.dll");
            FileInfo file2 = new FileInfo(@"c:\Windows\System32\mshtml.dll");
            ComparisonMethod comparisonMethod = ComparisonMethod.Byte;
            bool expected = false;
            bool actual = FileHelper.IsFileBackedUp(file1, file2, comparisonMethod);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsFileBackedUp
        ///</summary>
        [TestMethod()]
        public void IsFileBackedUpTest5()
        {
            FileInfo file1 = new FileInfo(@"c:\Windows\System32\imageres.dll");
            FileInfo file2 = new FileInfo(@"jhbjhbjkbh");
            ComparisonMethod comparisonMethod = ComparisonMethod.ArchiveBit;
            bool expected = false;
            bool actual = FileHelper.IsFileBackedUp(file1, file2, comparisonMethod);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsFileBackedUp
        ///</summary>
        [TestMethod()]
        public void IsFileBackedUpTest6()
        {
            FileInfo file1 = new FileInfo(@"c:\Windows\System32\imageres.dll");
            FileInfo file2 = new FileInfo(@"jhbjhbjkbh");
            ComparisonMethod comparisonMethod = ComparisonMethod.Modified;
            bool expected = false;
            bool actual = FileHelper.IsFileBackedUp(file1, file2, comparisonMethod);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void BackupFileTestCopy()
        {
            FileInfo sourceFile = new FileInfo(@"c:\Windows\System32\imageres.dll");
            DirectoryInfo backupDir = new DirectoryInfo(@"c:\SyncGurkeTestBackup");

            FileHelper.BackupFile(sourceFile, backupDir, 0, null);

            FileInfo backupFile = new FileInfo(@"c:\SyncGurkeTestBackup\imageres.dll");
            bool backupSuccess = FileHelper.IsFileBackedUp(sourceFile, backupFile, ComparisonMethod.Byte);

            backupDir.Delete(true);

            Assert.IsTrue(backupSuccess);
        }

        [TestMethod()]
        public void BackupFileTestVersioned()
        {
            FileInfo sourceFile = new FileInfo(@"c:\Windows\System32\imageres.dll");
            DirectoryInfo backupDir = new DirectoryInfo(@"c:\SyncGurkeTestBackup");
            DirectoryInfo versioningDir = new DirectoryInfo(@"c:\SyncGurkeTestVersions");

            if (backupDir.Exists)
                backupDir.Delete(true);
            if (versioningDir.Exists)
                versioningDir.Delete(true);

            FileHelper.BackupFile(sourceFile, backupDir, 1, versioningDir);

            bool nofirstVersionSuccess = versioningDir.EnumerateFiles().Count() == 0;

            FileHelper.BackupFile(sourceFile, backupDir, 1, versioningDir);

            FileInfo backupFile = new FileInfo(@"c:\SyncGurkeTestBackup\imageres.dll");
            bool backupSuccess = FileHelper.IsFileBackedUp(sourceFile, backupFile, ComparisonMethod.Byte);

            bool versioningSuccess = versioningDir.EnumerateFiles().First().Name.StartsWith("imageres.dll");

            if (backupDir.Exists)
                backupDir.Delete(true);
            if (versioningDir.Exists)
                versioningDir.Delete(true);

            Assert.IsTrue(backupSuccess);
            Assert.IsTrue(nofirstVersionSuccess);
            Assert.IsTrue(versioningSuccess);
        }
    }
}
